package com.huineta.business.common.security.process;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huineta.business.common.security.uutoken.SecurityUserTokenService;
import com.huineta.business.common.context.BaseContext;
import com.huineta.business.common.tools.core.ServletUtil;
import com.huineta.business.common.web.domain.Result;
import com.huineta.business.common.web.domain.ResultCode;
import com.huineta.business.common.constant.TokenConstant;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;

/**
 * Author:       Dmm Lee< bclassline@gmail.com >
 * Date:         2022/11/28
 * Description:  Security 注销成功处理类
 */
@Component
public class SecureLogoutSuccessHandler implements LogoutSuccessHandler {

    @Resource
    private BaseContext context;

    @Resource
    private SecurityUserTokenService customUserDetailsTokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException {
        String token = httpServletRequest.getHeader(TokenConstant.TOKEN_HEADER).replaceFirst(TokenConstant.TOKEN_PREFIX, "");
        customUserDetailsTokenService.destroyToken(token);
        ServletUtil.writeJson(Result.success(ResultCode.LOGOUT_SUCCESS));
    }
}
